Skip to content

MLE-32970: Send Connection: close on gzipped rows requests - #1995

Open
prgs-rsingh wants to merge 4 commits into
developfrom
MLE-32953-connection-close
Open

prgs-rsingh wants to merge 4 commits into
developfrom
MLE-32953-connection-close

Conversation

@prgs-rsingh

@prgs-rsingh prgs-rsingh commented Nov 24, 2026 •

Copy link
Copy Markdown

Problem

The released client (8.2.0) can store an empty document without reporting an error. A write with a body that can only be read once, such as an InputStreamHandle, can be sent on a connection that MarkLogic has already closed. The attempt fails after the body has been consumed, OkHttp silently sends the request again on a new connection with nothing left to send, and MarkLogic stores the empty body. In the same situation, a Data Services call with a streaming parameter (for example a Reader) fails intermittently.

There are three causes:

  1. Server bug (MLE-34772). MarkLogic answers a request that has both TE: trailers and Accept-Encoding: gzip with a chunked, gzipped response marked Connection: Keep-Alive, and then closes the connection. Every /v1/rows call from this client sends both headers, so OkHttp puts a dead connection back in its pool.
  2. A streamed body was sent twice. The client did not tell OkHttp that such a body can only be sent once, so OkHttp sent it again after a failure on a pooled connection.
  3. Idle connections. MarkLogic closes a connection that has been idle for 5 seconds. OkHttp kept idle connections for 5 minutes, and it only checks a pooled connection before a request with a body once the connection has been idle for 10 seconds. A connection that had been idle for 5 to 10 seconds could therefore be picked for a write. This does not depend on gzip or on the server bug.

Changes

  • ChunkedGzipConnectionCloseInterceptor adds Connection: close to HTTP/1.1 requests that have both headers, so OkHttp does not pool the connection. The server closes that connection anyway, so no connection reuse is lost. It is a network interceptor so that it sees the headers that are actually sent, including when gzip is disabled with RemoveAcceptEncodingConfigurator. This is a client workaround until the server is fixed.
  • Every request body is built by toRequestBody, so isOneShot() always reflects the handle's isResendable(), also inside a MultipartBody. OkHttp and RetryIOExceptionInterceptor then never resend a consumed body, and the real failure is reported instead of an empty document being written or Stream closed being thrown. RetryableRequestBody is deprecated; RetryIOExceptionInterceptor still honors it for bodies that do not override isOneShot().
  • With digest authentication, a body-less ping takes the 401 challenge before the first one-shot request on a thread. This is now done in sendRequestOnce, so it covers every call path. If the server is unavailable, the ping is retried within the same limits as any other retried request (com.marklogic.client.minimumRetries and com.marklogic.client.maximumRetrySeconds; 8 retries and 120 seconds by default). If it is still unavailable after that, the request fails with a FailedRetryException instead of sending a body that would be lost.
  • The connection pool shared by all clients (OkHttpUtil) now drops a connection after it has been idle for 4 seconds, instead of OkHttp's default of 5 minutes. MarkLogic closes the connection after 5 seconds anyway, so no connection reuse is lost when connecting to MarkLogic directly.

Behavior changes

  • Connections through a load balancer. A load balancer may keep an idle connection open for longer than 5 seconds. Those connections are now also dropped after 4 seconds and opened again when needed. An application that wants to keep them for longer can replace the pool by passing an OkHttpClientConfigurator that calls OkHttpClient.Builder#connectionPool to DatabaseClientFactory.addConfigurator. Configurators are applied after the default pool is set.
  • A failed request with a one-shot body is reported. It is no longer sent a second time by OkHttp, so the caller gets the exception from the failed attempt.
  • Waiting for an unavailable server before a one-shot request. This only applies to the first request on a thread with digest authentication, when the ping is answered with 502, 503 or 504. A streamed document write waits within the retry limits, as it did before. A Data Services call with a streaming parameter used to ping 10 times over about 1.5 seconds and then send the body anyway, and a resource read or write pinged once and sent the body regardless; both now wait within the same limits and fail without sending the body.

Tests

  • StreamingRequestOnReusedConnectionTest (MarkLogic): a document written from an InputStreamHandle arrives in full right after a gzipped rows request, and after the connection has been idle for 7 seconds with gzip disabled. Its clients do not have the retry interceptor that the other tests get from Common.
  • OkHttpUtilConnectionPoolTest (mock server): the shared pool drops a connection before it has been idle for 5 seconds, still reuses a connection that has just become idle, and can be replaced by a configurator.
  • StreamingPartRequestTest (mock server): a streaming request after a rows request succeeds; the connection is kept when gzip is disabled; a failed streaming request is not resent; digest authentication pings only before the first streaming request; a streaming request is not sent when the pings find the server unavailable for the configured retry period, and is sent once after the server becomes available.
  • ChunkedGzipConnectionCloseInterceptorTest (mock server): an HTTP/1.1 connection is not reused, an HTTP/2 connection is.
  • StreamingOutputImplTest: isOneShot() for each handle type and for multipart bodies; a body that only implements the deprecated RetryableRequestBody is still not retried.

Notes

  • Supersedes MLE-32970 Prevent retries of multipart streaming requests #1979, which is closed. This branch has the same commits plus the workaround and the pool change.
  • LockForUpdateTest has a timing fix that is unrelated to this change: the plan could run before the eval had taken the lock.
  • Not covered:
    • A response chunked through xdmp:set-response-chunked-encoding(true) cannot be detected from the request headers.
    • The 4 seconds assume the default keep-alive timeout of an app server, which is 5 seconds. With a timeout of 4 seconds or less, the pool has to be replaced as described above.
    • The client starts counting idle time when the application has finished reading a response, which can be later than when MarkLogic finished sending it. If the application takes more than about a second to read a response, the connection can still be closed by MarkLogic before the pool drops it. A request with a one-shot body then fails with an error; it is not written as an empty document.

Ryan Dew and others added 2 commits September 23, 2026 21:10
…ndability

StreamingOutputImpl previously hardcoded isRetryable()=false / isOneShot()=true
for every wrapped OutputStreamSender, even when the originating write handle
was fully buffered and resendable (e.g. JacksonHandle, StringHandle). This
meant any request body built from such a handle -- including Row Manager plan
execution, document writes, and Data Services parameters -- was needlessly
blocked from being retried by RetryIOExceptionInterceptor after a transient
connection error, even though the content could safely be written again.

Thread each call site's already-known isResendable()/handleBase.isResendable()
value into StreamingOutputImpl instead of assuming non-resendable:
- writeDocumentImpl-style document writes
- putPostValueImpl
- postResource / putResource (used by RowManagerImpl for Optic plan execution)
- postIteratedResourceImpl (multipart-mixed eval/invoke style resources)
- addParts multipart builder
- makeRequestBodyForContent and BaseProxy's makeRequestBody (Data Services
  single-node parameters)

Call sites without an existing resendable concept (structured query search,
alert match) keep the prior conservative false/one-shot default via new 3-arg
doPost/doPut overloads, so behavior is unchanged where resendability was
never determined.

Added StreamingOutputImplTest cases covering: a resendable body being retried
after a simulated connection failure, and a non-resendable part inside a
MultipartBody still propagating isOneShot()=true so it is never retried.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change ignores legacy retry opt-outs and uploads one-shot bodies after unsuccessful preflight retries, risking resends of consumed content and ResourceNotResendableException failures.

2 open issues
What changed in this PR

This PR addresses connection reuse and one-shot request handling in the core Java client, aiming to prevent failed uploads after MarkLogic closes a gzip-enabled rows connection.

Changes:

  • Adds a network interceptor that requests connection closure for gzip responses with trailers.
  • Uses OkHttp’s isOneShot() contract for retry decisions and centralizes digest-authentication preflight pings.
  • Closes buffered multipart responses promptly and adds streaming-request regression tests.
File Description
marklogic-client-api/​src/​test/​java/​com/​marklogic/​client/​impl/​StreamingPartRequestTest.java Adds connection reuse, streaming failure, and digest-authentication tests.
marklogic-client-api/​src/​test/​java/​com/​marklogic/​client/​impl/​StreamingOutputImplTest.java Tests handle resendability and multipart retry behavior.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​StreamingOutputImpl.java Reports one-shot status from handle resendability.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​OkHttpServices.java Centralizes body construction and preflight pings; closes buffered responses.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​RetryIOExceptionInterceptor.java Uses one-shot status to bypass retries.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​RetryableRequestBody.java Deprecates the legacy retry contract.
marklogic-client-api/​src/​main/​java/​com/​marklogic/​client/​impl/​okhttp/​ChunkedGzipConnectionCloseInterceptor.java Adds the gzip/trailers connection-close workaround.

Review effort: Balanced

@prgs-rsingh
prgs-rsingh force-pushed the MLE-32953-connection-close branch from a41b4a6 to d100c10 Compare November 24, 2026 04:41
@prgs-rsingh prgs-rsingh changed the title Mle 32953 connection close MLE-32953 Send Connection: close on gzipped rows requests Nov 24, 2026
@prgs-rsingh
prgs-rsingh force-pushed the MLE-32953-connection-close branch from d100c10 to 10c78a5 Compare November 24, 2026 04:48
@hmadala001
hmadala001 force-pushed the MLE-32953-connection-close branch from 0eaf4a4 to 8e52d57 Compare December 1, 2026 06:22
MarkLogic answers a request that has both "TE: trailers" and
"Accept-Encoding: gzip" (every /v1/rows call) with a chunked, gzipped
response marked "Connection: Keep-Alive", and then closes the
connection. OkHttp reuses that dead connection, so a following request
with a one-shot body (e.g. a Reader) fails and cannot be resent. A Data
Services call with a streaming parameter, made right after
RowManager.resultRows(), failed this way with "Stream closed".

- ChunkedGzipConnectionCloseInterceptor adds "Connection: close" to
  such requests on HTTP/1.1, so OkHttp does not pool the connection.
  This is a workaround until the server bug is fixed.
- Every request body is built by toRequestBody, so isOneShot() always
  reflects the handle's isResendable(), also inside a MultipartBody.
  OkHttp and RetryIOExceptionInterceptor then never resend a consumed
  body. RetryableRequestBody is deprecated; RetryIOExceptionInterceptor
  still honors it for bodies that do not override isOneShot().
- With digest authentication, a body-less ping takes the 401 challenge
  before the first one-shot request on a thread. This is now done in
  sendRequestOnce, so it covers every call path. If the server is still
  unavailable after the ping retries, the request fails with a
  FailedRetryException instead of sending a body that would be lost.

StreamingPartRequestTest and ChunkedGzipConnectionCloseInterceptorTest
use a mock server. StreamingOutputImplTest checks isOneShot() for each
handle type.

MLE-32953

MLE-32953 Test the connection pool keep-alive without MarkLogic

- OkHttpUtilConnectionPoolTest verifies against a mock server that the
  shared pool drops a connection before it has been idle for 5 seconds,
  still reuses one that has just become idle, and can be replaced by an
  OkHttpClientConfigurator.
- The comment on the pool states that the 4 seconds assume the default
  keep-alive timeout of an app server and how to replace the pool.
- StreamingRequestOnReusedConnectionTest removes the retry interceptor
  that Common adds, so that its clients match an application's client.

Update LockForUpdateTest.java
@hmadala001
hmadala001 force-pushed the MLE-32953-connection-close branch from 8e52d57 to b25f2f2 Compare December 1, 2026 06:22
…mits

- The ping that precedes the first one-shot request with digest
  authentication is retried within the configured retry limits (at
  least 8 retries and up to 120 seconds by default) with the usual
  backoff, instead of 10 times about 150 ms apart. A streamed document
  write already waited that long before this branch.
- StreamingPartRequestTest covers the limits, and a server that becomes
  available after a few pings.
- LockForUpdateTest: the copyright year and the assertion message match
  the change that was made to the test.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Shared connection-pool and digest preflight changes affect every client and require deployed-server validation of MarkLogic connection-closing and load-balancer interactions, which was unavailable during review.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

@NiteshMehta NiteshMehta changed the title MLE-32953 Send Connection: close on gzipped rows requests MLE-32970: Send Connection: close on gzipped rows requests Dec 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants